package com.smfx.yxt.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.enums.RelationType;
import com.smfx.yxt.daoSqlserver.AttachmentListMapper;
import com.smfx.yxt.daoSqlserver.ClassAlbumMapper;
import com.smfx.yxt.daoSqlserver.ParentInfoMapper;
import com.smfx.yxt.model.AttachmentList;
import com.smfx.yxt.model.ClassAlbum;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.ParentReqVO;
import com.smfx.yxt.service.IClassAlbumService;
import com.smfx.yxt.utils.CommonUtil;
import com.smfx.yxt.view.ClassAlbumView;
import com.smfx.yxt.view.ParentRelStudentView;
import com.smfx.yxt.wechat.utils.TemplateUtil;

@Service("classAlbumService")
public class ClassAlbumServiceImpl implements IClassAlbumService {

	@Autowired
	private ClassAlbumMapper classAlbumMapper;
	@Autowired
	private AttachmentListMapper attachmentListMapper; 
	@Autowired
	private ParentInfoMapper parentInfoMapper;
	
	
	@Override
	public int insertSelective(ClassAlbum record) {
		return classAlbumMapper.insertSelective(record);
	}

	@Override
	public ClassAlbum selectByPrimaryKey(Integer id) {
		return classAlbumMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(ClassAlbum record) {
		return classAlbumMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<ClassAlbumView> selectListByPage(Page<ClassAlbumView> page) {
		return classAlbumMapper.selectListByPage(page);
	}

	@Override
	public ClassAlbum createClassAlbum(ClassAlbum classAlbum) {
		classAlbumMapper.insertSelective(classAlbum);
		AttachmentList attachment = new AttachmentList();
		attachment.setTeacherId(classAlbum.getTeacherId());
		attachment.setRelationType(RelationType.CLASS_ALBUM.getCode());
		attachment.setRelationId(Long.valueOf(classAlbum.getId()));
		attachmentListMapper.updateRelationId(attachment);
		new Thread(new ClassAlbumNoticeExecutor(classAlbum)).start();
		return classAlbum;
	}
	
	class ClassAlbumNoticeExecutor implements Runnable {
		
		private ClassAlbum classAlbum;
		
		public ClassAlbumNoticeExecutor(ClassAlbum classAlbum) {
			this.classAlbum = classAlbum;
		}

		@Override
		public void run() {
			try { 
				String content = classAlbum.getContent();
				if(content.length() > 50) {
					content = content.substring(0, 50) + "...";
				}
				String gradeClass = CommonUtil.getGradeClass(classAlbum.getStudyType(), classAlbum.getGrade(), classAlbum.getClazz());
				String reqUrl = Const.BASEPATH + Const.STUDENT_ROLE + MenuConst.ABLUM_CLASS_URL + "?sid=";
				ParentReqVO reqVO = new ParentReqVO();
				reqVO.setGrade(classAlbum.getGrade());
				reqVO.setClazz(classAlbum.getClazz());
				reqVO.setSchoolId(classAlbum.getSchoolId());
				List<ParentRelStudentView> list = parentInfoMapper.selectListForClass(reqVO); 
				for(ParentRelStudentView parent : list) {
					String currReqUrl = reqUrl + parent.getStudentId();
					TemplateUtil.classAlbumNoticeSend(parent.getOpenid(), parent.getName(), gradeClass, classAlbum.getTeacherName(), content, currReqUrl);
				}
			} catch (Exception e) { 
	            e.printStackTrace(); 
	        }
		}
	}

	@Override
	public ClassAlbumView selectViewById(Integer id) {
		return classAlbumMapper.selectViewById(id);
	}

}
